import logging

import requests
from exceptiongroup import catch


class RequestUtil:
    """
    基于 request 库的 http 请求工具类
    Get:
    Post:
    Put:
    Delete:
    """
    def __init__(self):
        logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
        logging.info('httpUtil 初始化成功')

    @staticmethod
    def get(url, headers=None, timeout=10):
        """
        发送 get请求，请求参数在 url 的方式
        :param url: 请求地址
        :param headers: 请求头
        :param timeout: 请求超时时间
        :return: 返回字典
        """
        try:
            response = requests.get(url, headers=headers, timeout=timeout)
            response.raise_for_status()
            return response.json()
        except requests.RequestException as e:
            logging.error(e)
            return None

    @staticmethod
    def get_with_params(url, headers=None, params=None, timeout=10):
        """
        发送 get请求，带params的方式
        :param url: 请求地址
        :param headers: 请求头
        :param params: 请求参数
        :param timeout: 超时时间，默认10秒
        :return: 返回字典
        """
        try:
            response = requests.get(url, headers=headers, params=params, timeout=timeout)
            response.raise_for_status()
            return response.json()
        except requests.RequestException as e:
            logging.error(e)
            return None

    @staticmethod
    def post(url, headers=None, data=None, timeout=10):
        """
        发送 post请求
        :param url: 请求地址
        :param headers: 请求头
        :param data: 请求体
        :param timeout: 超时时间
        :return: 返回字典
        """
        try:
            response = requests.post(url,headers=headers, data=data, timeout=timeout)
            response.raise_for_status()
            return response.json()
        except requests.RequestException as e:
            logging.error(e)
            return None

    @staticmethod
    def put(url, headers=None, body=None, timeout=10):
        """
        发送 put请求
        :param url: 请求地址
        :param headers: 请求头
        :param body: 请求体
        :param timeout: 超时时间
        :return: 返回字典
        """
        try:
            response = requests.put(url, headers=headers, data=data, timeout=timeout)
            response.raise_for_status()
            return response.json()
        except requests.RequestException as e:
            logging.error(e)
            return None

    @staticmethod
    def delete(url, headers=None, timeout=10):
        """
        发送 delete请求
        :param url: 请求地址
        :param headers: 请求头
        :param timeout: 超时时间
        :return: True 或者 False
        """
        try:
            response = requests.get(url, headers=headers, timeout=timeout)
            response.raise_for_status()
            return True
        except requests.RequestException as e:
            logging.error(e)
            return False


if __name__ == '__main__':
    http = RequestUtil()
    post_data = {'key': '5a112d4ccf4d07ea3e0cbc884ccd429c', 'word': '鸡块', 'num': '100', 'page': '10'}
    # result = http.get('https://apis.juhe.cn/fapig/soup/query?key=fddb96963ca4759e3db4cb8b56cdadf4')
    # result = http.get_with_params('https://apis.juhe.cn/fapig/soup/query', {'key': 'fddb96963ca4759e3db4cb8b56cdadf4'})
    result = http.post('http://apis.juhe.cn/fapigx/caipu/query', data=post_data)
    logging.info(result)
